#include <unistd.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>

void print_usage()
{
	printf("you don't know what you are doing\n");
	printf("please press w, a, s, or d to make the robot do something\n");
}

/* initialize new terminal i/o settings */
static struct termios old, new1;
void initTermios(int echo)
{
	tcgetattr(0, &old); /* grab old terminal i/o settings */
	new1 = old; /* make new settings same as old settings */
	new1.c_lflag &= ~ICANON; /*disable buffered i/o */
	new1.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
	tcsetattr(0, TCSANOW, &new1); /* use these new terminal i/o settings now */
}

/* Restore old terminal i/o settings */

void resetTermios(void) {
	tcsetattr(0, TCSANOW, &old);
}

int main()
{
	char c;
	initTermios(0);
	printf("if you want the program to do something, press w, a, s, or d\n");
	while (1 && black_button() == 0)
	{
		read(0, &c, 1); 
		switch(c)
		{
			case 'w': /* Drive Forward */
				mav(0, 1000);
				mav(3, 1000);
				break;
			case 's': /* Drive Backwards */
				mav(0, -1000);
				mav(3, -1000);
				break;
			case 'a': /* Turn Left */
				mav(0, -1000);
				mav(3, 1000);
				break;
			case 'd': /* Turn Right */
				mav(0, 1000);
				mav(3, -1000);
				break;
			case 'p': /* slam that in the trunk */
				ao();
				break;
			case '/': /* kill yourself */
				ao();
				resetTermios();
				return 0;
				break;
			case 'y': /* ride dirty */
				system("aplay /mnt/user/code/00_ridin_39_dirty_clean_edit_youtube_unknown.wav &");
				break;
			case 'u': /* kill the music player */
				system("killall aplay &");
				break;
			default: 
				print_usage();
				break;
		}
	}
	ao();
	resetTermios();
	return 0;
}
